package com.shishuo.cms.action;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.shishuo.cms.entity.Attachment;
import com.shishuo.cms.entity.Folder;
import com.shishuo.cms.exception.FolderNotFoundException;
import com.shishuo.cms.exception.TemplateNotFoundException;

@Controller
@RequestMapping({ "/photo" })
public class AttachmentAction extends BaseAction{
	
	@RequestMapping(value = "/{attachmentId}.htm", method = RequestMethod.GET)
	public String home(@RequestParam(value = "p", defaultValue = "1") long p,
			@PathVariable long attachmentId,
			ModelMap modelMap) {
		try {
			Attachment attachment=attachmentService.getAttachmentById(attachmentId);
			if (null!=attachment) {
				Folder folder=folderService.getFolderById(attachment.getKindId());
				List<Folder> folderPathList = folderService.getFolderPathListByFolderId(attachment.getKindId());
				modelMap.put("folderPathList", folderPathList);
				modelMap.put("folder", folder);
			}
			modelMap.put("attachment", attachment);
			return themeService.getPhotoDetailTemplatePath("photo-detail");
		} catch (FolderNotFoundException e) {
			logger.fatal(e.getMessage());
			return themeService.getTemplatePath("404");
		}catch (TemplateNotFoundException e) {
			logger.fatal(e.getMessage());
			return themeService.getTemplatePath("404");
		}
	}
}
